module fiona_mod

  use fiona_common_mod
  use fiona_dim_mod
  use fiona_var_mod
  use fiona_dataset_mod
  use fiona_objects_mod
  use fiona_input_mod
  use fiona_output_mod

  implicit none

  private

  public fiona_init
  public fiona_set_time
  public fiona_final
  public fiona_create_dataset
  public fiona_has_dataset
  public fiona_open_dataset
  public fiona_add_att
  public fiona_add_dim
  public fiona_add_var
  public fiona_has_var
  public fiona_start_output
  public fiona_output
  public fiona_end_output
  public fiona_start_input
  public fiona_set_dim
  public fiona_get_dim
  public fiona_get_att
  public fiona_input
  public fiona_input_range
  public fiona_end_input

contains

  subroutine fiona_init(time_units, start_time)

    character(*), intent(in), optional :: time_units
    character(*), intent(in), optional :: start_time

    logical, save :: called = .false.

    if (called) return
    called = .true.

    if (present(time_units)) then
      time_units_str = time_units
      select case (time_units)
      case ('days', 'sol')
        time_units_in_seconds = 86400.0
      case ('hours')
        time_units_in_seconds = 3600.0
      case ('minutes')
        time_units_in_seconds = 60.0
      case ('seconds')
        time_units_in_seconds = 1.0
      case default
        call log_error('Invalid time_units ' // trim(time_units) // '!', __FILE__, __LINE__)
      end select
    end if

    if (present(start_time)) then
      start_time_str = start_time
    end if

    call datasets%init()

  end subroutine fiona_init

  subroutine fiona_set_time(time_units, start_time)

    character(*), intent(in) :: time_units
    character(*), intent(in) :: start_time

    time_units_str = time_units
    select case (time_units)
    case ('days', 'sol')
      time_units_in_seconds = 86400.0
    case ('hours')
      time_units_in_seconds = 3600.0
    case ('minutes')
      time_units_in_seconds = 60.0
    case ('seconds')
      time_units_in_seconds = 1.0
    case default
      call log_error('Invalid time_units ' // trim(time_units) // '!', __FILE__, __LINE__)
    end select

    start_time_str = start_time

  end subroutine fiona_set_time

  subroutine fiona_final()

    call datasets%clear()

  end subroutine fiona_final

end module fiona_mod
